package com.HuangJiaQing.www.controller;

import com.HuangJiaQing.www.util.CartPlus;
import com.HuangJiaQing.www.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author HONOR
 * @date 2022/7/29 15:12
 */
@Controller
public class CartController {
    @Autowired
    private CartService cartService;


    @RequestMapping(value = "/addCart/{id}",method = RequestMethod.GET)
    public String addCart(@PathVariable("id") Integer id){
        String s = cartService.addCart(id);
        return s;
    }

    @RequestMapping(value = "/toCart",method = RequestMethod.GET)
    public String toCart(Model model){
        List<CartPlus> list = cartService.selectCartAndSort();
        model.addAttribute("arrayList",list);
        return "cart";
    }

    @RequestMapping(value = "/updateGoodsNumber/{id}",method = RequestMethod.PUT)
    public String updateGoodsNumber(@PathVariable("id") Integer id,int number){
      cartService.updateGoodsNumber(id,number);
      return "redirect:/toCart";
    }

    @RequestMapping(value = "/deleteCart/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public String deleteCart(@PathVariable("id")Integer id){
        cartService.deleteCartById(id);
        return "success";
    }



}
